package com.ruoyi.flowable.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.flowable.domain.bo.AppBo;
import com.ruoyi.flowable.domain.vo.*;
import com.ruoyi.flowable.service.*;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front")
public class FrontController extends BaseController {

    @Autowired
    private ISysPostService postService;

    @Autowired
    private IAppService appService;
    @Autowired
    private IAppVersionService appVersionService;

//    @Autowired
//    private ISysUserService userService;

    @Autowired
    private IFormService formService;

    @Autowired
    private IFormVersionService formVersionService;

    @Autowired
    private IFormDataService formDataService;

    @Autowired
    private IBpmnConfigService bpmnConfigService;


    @GetMapping(value = "/listApp")
    public R<List<AppVo>> listApp() {
        //TODO 过滤条件
        AppBo app = new AppBo();
        app.setStatus("0");
        return R.ok(appService.queryList(app));
    }

    @GetMapping(value = "/getApp/{appId}")
    public R<AppVo> getApp(@PathVariable("appId") Long appId) {
        return R.ok(appService.queryById(appId));
    }

    @GetMapping(value = "/getForm/{formId}")
    public R<FormVo> getForm(@PathVariable("formId") Long formId) {
        return R.ok(formService.queryById(formId));
    }

    @GetMapping(value = "/getAppVersion/{appVersionId}")
    public R<AppVersionVo> getAppVersion(@PathVariable("appVersionId") Long appVersionId) {
        return R.ok(appVersionService.queryById(appVersionId));
    }

    @GetMapping(value = "/getFormVersion/{formVersionId}")
    public R<FormVersionVo> getFormVersion(@PathVariable("formVersionId") Long formVersionId) {
        return R.ok(formVersionService.queryById(formVersionId));
    }

    @GetMapping(value = "/getBpmnConfig/{appVersionId}")
    public R<BpmnConfigVo> getBpmnConfig(@PathVariable("appVersionId") Long appVersionId) {
        return R.ok(bpmnConfigService.selectBpmnConfigByAppVersionId(appVersionId));
    }

    @GetMapping(value = "/getDataByInstId/{procInstId}")
    public R<?> getFormDataByInstId(@PathVariable("procInstId") String procInstId) {
        FormDataVo formData = formDataService.selectFormDataByInstId(procInstId);
        if (formData == null) {
            return R.fail("发起数据不存在");
        }
        FormVersionVo formVersion = formVersionService.queryById(formData.getFormVersionId());
        AppVersionVo appVersion = appVersionService.queryById(formData.getAppVersionId());
        BpmnConfigVo bpmnConfig = bpmnConfigService.selectBpmnConfigByAppVersionId(formData.getAppVersionId());
        Map<String, Object> res = new HashMap<>();
        res.put("formData", formData);
        res.put("formVersion", formVersion);
        res.put("appVersion", appVersion);
        res.put("bpmnConfig", bpmnConfig);
        return R.ok(res);
    }

    @GetMapping("/listPost")
    public R<List<SysPost>> listPost() {
        return R.ok(postService.selectPostAll());
    }

}
